package com.example.camera2sample;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class MainActivity extends AppCompatActivity {

    private static final  int REQUEST_PERMISSION_CODE  = 1;

    private Set<String> REQUEST_PERMISSIONS;

    private CameraManager cameraManager;

    private String[] cameraIdList;

    private String frontCameraId;
    private String backCameraId;

    private CameraCharacteristics frontCameraCharacteristics;

    private CameraCharacteristics backCameraCharacteristics;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRequestPermissions();
        checkPermissions();
        initCameraParameters();

    }


    private void initCameraParameters(){
        cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
        try {
            cameraIdList = cameraManager.getCameraIdList();
            for(String cameraId: cameraIdList){
                CameraCharacteristics cameraCharacteristics = cameraManager
                        .getCameraCharacteristics(cameraId);
                if(isHardWareLevelSupported(cameraCharacteristics.get(CameraCharacteristics.INFO))){

                }
            }

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }


    }

    private boolean isHardWareLevelSupported(int requiredLevel){
        int[] sortedLevels = new int[]{
                CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY,
                CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED,
                CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL,
                CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3};
        for()

        return true;
    }



    private void initRequestPermissions(){
        REQUEST_PERMISSIONS = new TreeSet<>();
        REQUEST_PERMISSIONS.add(Manifest.permission.CAMERA);
        REQUEST_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    }



    private boolean checkPermissions(){
        Set<String> deniedPermissions = new TreeSet<>();
        for(String permission : REQUEST_PERMISSIONS){
            if(ContextCompat.checkSelfPermission(this,permission) ==
                    PackageManager.PERMISSION_DENIED){
                deniedPermissions.add(permission);
            }
        }
        if(!deniedPermissions.isEmpty()){
            requestPermissions(deniedPermissions);
        }

        return deniedPermissions.isEmpty();
    }


    private void requestPermissions(Set<String> deniedPermissions){
        String[] permissions = new String[10];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions( deniedPermissions.toArray(permissions),REQUEST_PERMISSION_CODE);
        }
    }




    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == REQUEST_PERMISSION_CODE){
            if(grantResults.length>1&& grantResults[0] == PackageManager.PERMISSION_GRANTED){

            }else {
                Toast.makeText(this,"获取权限失败",Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }
}
